package com.huawei.spare.part.replace.models;

import io.reactivex.disposables.Disposable;
import javafx.beans.property.*;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

public class DownloadTask implements Serializable {

    ObjectProperty<InstallPackage> installPackage;
    StringProperty software;
    StringProperty version;
    StringProperty packageName;
    StringProperty url;
    DoubleProperty progress;
    ObjectProperty<Status> status;

    // downloading observable
    @Getter @Setter
    Disposable disposable;

    @Getter @Setter
    String localFileUrl;

    public DownloadTask(InstallPackage installPackage, String software, String version, String downloadUrl) {
        this.installPackage = new SimpleObjectProperty<>(installPackage);
        this.packageName = new SimpleStringProperty(installPackage == null ? null : installPackage.getPackageName());
        this.status = new SimpleObjectProperty<>(Status.Pending);
        this.software = new SimpleStringProperty(software);
        this.version = new SimpleStringProperty(version);
        this.progress = new SimpleDoubleProperty(0);
        this.url = new SimpleStringProperty(downloadUrl);
    }

    public ObjectProperty<InstallPackage> installPackageProperty() {
        return installPackage;
    }

    public StringProperty softwareProperty() {
        return software;
    }

    public StringProperty versionProperty() {
        return version;
    }

    public DoubleProperty progressProperty() {
        return progress;
    }

    public StringProperty packageNameProperty() {
        return packageName;
    }

    public StringProperty urlProperty() {
        return url;
    }

    public ObjectProperty<Status> statusProperty() {
        return status;
    }

    public void dispose() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    public enum Status {
        Pending, Downloading, Completed;
    }
}
